package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.weight.view.MyListView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-23 11:54:44
 * @copyRight http://www.qiyunxin.com
 * @function 子部门
 */
public class SubsectorsActivity extends Activity {

	private MyListView employee_listview, department_listview;
	private DepartmentsAdapter mDepartmentsAdapter = null;
	private EmployeeAdapter mEmployeeAdapter = null;
	// private String superDepName;
	// private int entid = 0, pid = 0;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private DepartmentEntity departmentEntity = null;
	private boolean is_manager = false;
	private LinearLayout bottom_layout, no_employee_layout;
	private boolean has_department = true, has_employee = true;
	private View loading;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.subsectors_layout);
		departmentEntity = (DepartmentEntity) getIntent().getSerializableExtra(
				"departmentEntity");
		is_manager = getIntent().getBooleanExtra("is_manager", false);
		// superDepName = getIntent().getStringExtra("superior_department");
		// entid = getIntent().getIntExtra("entid", 0);
		// pid = getIntent().getIntExtra("pid", 0);
		initView();
		initListener();
		loading.setVisibility(View.VISIBLE);
		getdata();
		getCustByDepart();
	}

	protected void initView() {
		loading = findViewById(R.id.loading);
		TextView title = (TextView) findViewById(R.id.title_textview);
		employee_listview = (MyListView) findViewById(R.id.employee_listview);
		department_listview = (MyListView) findViewById(R.id.department_listview);
		bottom_layout = (LinearLayout) findViewById(R.id.bottom_layout);
		no_employee_layout = (LinearLayout) findViewById(R.id.no_employee_layout);
		// if (!TextUtils.isEmpty(superDepName)) {
		// title.setText(superDepName);
		// }
		if (departmentEntity != null) {
			title.setText(departmentEntity.departname);
		}
		if (is_manager) {
			bottom_layout.setVisibility(View.VISIBLE);
		}
	}

	protected void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.add_employee_tv).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO 添加员工
						Intent intent = new Intent(SubsectorsActivity.this,
								AddEmployeeActivity.class);
						intent.putExtra("entid", departmentEntity.entid);
						intent.putExtra("departid", departmentEntity.departid);
						intent.putExtra("superior_department",
								departmentEntity.departname);
						startActivityForResult(intent, 100);
					}
				});
		findViewById(R.id.add_department_tv).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO 添加子部门
						Intent intent = new Intent(SubsectorsActivity.this,
								CreateDepartmentsActivity.class);

						DepartmentEntity mDepartmentEntity = new DepartmentEntity();
						mDepartmentEntity.entid = departmentEntity.entid;
						mDepartmentEntity.pid = departmentEntity.departid;
						mDepartmentEntity.pdepartname = departmentEntity.departname;
						intent.putExtra("departmentEntity", mDepartmentEntity);
						startActivityForResult(intent, 100);
					}
				});

		findViewById(R.id.department_manager_tv).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						/** 编辑部门 **/
						Intent intent = new Intent(SubsectorsActivity.this,
								CreateDepartmentsActivity.class);
						intent.putExtra("is_manager", is_manager);
						intent.putExtra("departmentEntity", departmentEntity);
						startActivityForResult(intent, 100);

					}
				});
		department_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DepartmentEntity mDepartmentEntity = (DepartmentEntity) arg0
						.getAdapter().getItem(arg2);
				if (mDepartmentEntity != null) {
					Intent intent = new Intent(SubsectorsActivity.this,
							SubsectorsActivity.class);
					intent.putExtra("departmentEntity", mDepartmentEntity);
					intent.putExtra("is_manager", is_manager);
					startActivityForResult(intent, 100);
				}
			}

		});
		employee_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (is_manager) {
					EmployeeEntity mEmployeeEntity = (EmployeeEntity) arg0
							.getAdapter().getItem(arg2);
					if (mEmployeeEntity != null) {
						Intent intent = new Intent(SubsectorsActivity.this,
								AddEmployeeActivity.class);
						intent.putExtra("entid", departmentEntity.entid);
						intent.putExtra("departid", departmentEntity.departid);
						intent.putExtra("employeeEntity", mEmployeeEntity);
						intent.putExtra("superior_department",
								departmentEntity.departname);
						startActivityForResult(intent, 100);

					}
				}else{
					EmployeeEntity mEmployeeEntity = (EmployeeEntity) arg0
							.getAdapter().getItem(arg2);
					if (mEmployeeEntity != null) {
						ActivityHandle.startUserDetailActivity(SubsectorsActivity.this, mEmployeeEntity.custid);
					}
				}
			}
		});
	}

//	private void deleteEmployeeDialog(){
//
//	}
	/**
	 * 获取该部门下的子部门
	 */
	private void getdata() {
		mCompanyHandle.getDepartByPidList(departmentEntity.departid,
				departmentEntity.entid, new CompanyHandle.IGetDepartListResult() {

					@Override
					public void onGetDepartResult(int status, String msg,
							ArrayList<DepartmentEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (arrayList != null && arrayList.size() > 0) {
								has_department = true;
								mDepartmentsAdapter = new DepartmentsAdapter(
										SubsectorsActivity.this, arrayList,false);
								department_listview
										.setAdapter(mDepartmentsAdapter);
							} else {
								has_department = false;
							}
							checkStatus();
						}
					}
				});
	}

	/**
	 * 获取该部门下成员
	 */
	private void getCustByDepart() {
		mCompanyHandle.getCustbyDepart(departmentEntity.departid,
				departmentEntity.entid, new CompanyHandle.IGetCustByDepartListener() {

					@Override
					public void onGetCustByDepartResult(int status, String msg,
							ArrayList<EmployeeEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (arrayList != null && arrayList.size() > 0) {
							has_employee = true;
							mEmployeeAdapter = new EmployeeAdapter(
									SubsectorsActivity.this, arrayList,
									is_manager);
							employee_listview.setAdapter(mEmployeeAdapter);
						} else {
							has_employee = false;
						}
						checkStatus();
					}
				});
	}

	private void checkStatus() {
		if (!has_department && !has_employee) {
			no_employee_layout.setVisibility(View.VISIBLE);
		} else {
			no_employee_layout.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			getdata();
			getCustByDepart();
		} else if (requestCode == 100 && resultCode == 2) {
			setResult(3);
			finish();
		} else if (requestCode == 100 && resultCode == 3) {
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
